
package com.hulk.ratel.cloud.filter;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.hulk.ratel.common.constant.enums.BussErrCode;
import com.hulk.ratel.common.exception.BaseRteException;

import com.hulk.ratel.component.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * cloud 过滤器
 *
 * @author hulk
 */
//@Slf4j
//@Component
//@Order(Ordered.LOWEST_PRECEDENCE-1)
//@WebFilter(filterName = "cloudFilter", urlPatterns = "/cloud/*")
//public class CloudFilter extends OncePerRequestFilter {
//
//    //TODO  feign 拦截器要特殊处理
//
//
//    @Autowired
//    private ObjectMapper json;
//
//    @Override
//    public void doFilterInternal(HttpServletRequest request, HttpServletResponse resp,
//                         FilterChain chain) throws ServletException, IOException {
//
//        try {
//            HttpServletRequest req = (HttpServletRequest) request;
//            chain.doFilter(new RequestWrapper(req), resp);
//        } catch (BaseRteException e) {
//            resp.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
//            resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
//            PrintWriter wr = resp.getWriter();
//            wr.write(json.writeValueAsString(R.fail(e.getErrorCode(), e.getErrorMessage())));
//            wr.flush();
//            wr.close();
//        } catch (Exception e) {
//            resp.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
//            resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
//            PrintWriter wr = resp.getWriter();
//            wr.write(json.writeValueAsString(R.fail(BussErrCode.E_999999.getErrCode(), BussErrCode.E_999999.getErrDesc())));
//            wr.flush();
//            wr.close();
//        }
//    }
//
//
//}
